//
//  OMGSiteViewController.m
//  A_project2.0
//
//  Created by Tiny on 16/3/7.
//  Copyright © 2016年 七平. All rights reserved.
//

#import "OMGSiteViewController.h"
#import "OMGSiteModel.h"
#import "OMGSIteTableViewCell.h"
#import "OMGSiteCollectionViewCell.h"
#import "OMCollectionViewController.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height


@interface OMGSiteViewController ()<UITableViewDataSource,UITableViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic,strong)NSMutableArray *hot_countryArr;//热门城市数组
@property (nonatomic, strong) NSMutableArray *country;//下面的tableViewCell的城市数组
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UICollectionView *collecionView;

@end

@implementation OMGSiteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"目的地";
    self.view.backgroundColor = [UIColor whiteColor];
    [self dataHander:@"10"];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
        [_tableView registerClass:[OMGSIteTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    
    
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 640)];
    self.tableView.tableHeaderView = _headerView;
#pragma mark -  header上的uiview
    UIImageView *picView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width , 250)];
//    picView.backgroundColor = [UIColor greenColor];
    picView.image = [UIImage imageNamed:@"ditu.jpg"];
    [self.headerView addSubview:picView];
#pragma mark -  七大洲下面的热门目的地
    UILabel *hotLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 260, 100, 30)];
    hotLabel.text = @"热门目的地";
    hotLabel.font = [UIFont systemFontOfSize: 18];
    [self.headerView addSubview:hotLabel];
#pragma mark -  collectionView 下面的label 其他目的地
    UILabel *otherLabel =  [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 8 - 20, 600, 120, 30)];
    [self.headerView addSubview:otherLabel];
    otherLabel.text = @"其他目的地";
    otherLabel.font = [UIFont systemFontOfSize:17];
    
    
#pragma mark -  collectionView 下面的label 拼音首字母排序
    UILabel *pinyinLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH - 115, 600, WIDTH - 7, 30)];
    [self.headerView addSubview:pinyinLabel];
    pinyinLabel.text = @"拼音首字母排序";
    pinyinLabel.font = [UIFont systemFontOfSize:15];
    pinyinLabel.textColor = [UIColor colorWithRed:85 / 255.0 green:85 / 255.0 blue:85 / 255.0 alpha:1];
    
    

#pragma mark -  七大洲点击按钮 button
    UIButton *asia = [UIButton buttonWithType:UIButtonTypeCustom];
    asia.frame = CGRectMake(WIDTH / 2 + 85, 20, WIDTH / 9, WIDTH / 9
                            );
    //    asia.backgroundColor = [UIColor colorWithRed:236 / 255.0 green:236 / 255.0 blue:236 / 255.0 alpha:1];
    [asia setTitle:@"亚洲" forState:UIControlStateNormal];
    asia.titleLabel.font = [UIFont systemFontOfSize:16];
    [asia addTarget:self action:@selector(Asia1) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:asia];
    UIButton *europe = [UIButton buttonWithType:UIButtonTypeCustom];
    europe.frame = CGRectMake(WIDTH / 2 + 15, 30, WIDTH / 9, WIDTH / 9);
    //    europe.backgroundColor = [UIColor greenColor];
    [europe setTitle:@"欧洲" forState:UIControlStateNormal];
    europe.titleLabel.font = [UIFont systemFontOfSize:16];
    [europe addTarget:self action:@selector(Europe) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:europe];
    
    UIButton *africa = [UIButton buttonWithType:UIButtonTypeCustom];
    africa.frame = CGRectMake(WIDTH / 2 + 15, 150, WIDTH / 9, WIDTH / 9);
    //    africa.backgroundColor = [UIColor whiteColor];
    africa.titleLabel.font = [UIFont systemFontOfSize:16];
    [africa setTitle:@"非洲" forState:UIControlStateNormal];
    [africa addTarget:self action:@selector(Africa) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:africa];
    
    UIButton *americaN = [UIButton buttonWithType:UIButtonTypeCustom];
    americaN.frame = CGRectMake(WIDTH / 9, 30, WIDTH / 6, WIDTH / 9);
    //    americaN.backgroundColor = [UIColor yellowColor];
    [americaN setTitle:@"北美洲" forState:UIControlStateNormal];
    americaN.titleLabel.font = [UIFont systemFontOfSize:16];
    [americaN addTarget:self action:@selector(AmericaN) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:americaN];
    
    UIButton *americaS = [UIButton buttonWithType:UIButtonTypeCustom];
    americaS.frame = CGRectMake(WIDTH / 3 - 25, 120, WIDTH / 6, WIDTH / 9);
    //    americaS.backgroundColor = [UIColor greenColor];
    [americaS setTitle:@"南美洲" forState:UIControlStateNormal];
    americaS.titleLabel.font = [UIFont systemFontOfSize:16];
    [americaS addTarget:self action:@selector(AmericaS) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:americaS];
    
    UIButton *oceania = [UIButton buttonWithType:UIButtonTypeCustom];
    oceania.frame = CGRectMake(WIDTH - 65, 150, WIDTH / 6, WIDTH / 9);
    //    oceania.backgroundColor = [UIColor blackColor];
    [oceania setTitle:@"大洋洲" forState:UIControlStateNormal];
    oceania.titleLabel.font = [UIFont systemFontOfSize:16];
    
    [oceania addTarget:self action:@selector(Oceania) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:oceania];
    
    UIButton *antarctica = [UIButton buttonWithType:UIButtonTypeCustom];
    antarctica.frame = CGRectMake(WIDTH / 3 + 25, 210, WIDTH / 6, WIDTH / 9);
    //    antarctica.backgroundColor = [UIColor yellowColor];
    [antarctica setTitle:@"南极洲" forState:UIControlStateNormal];
    antarctica.titleLabel.font = [UIFont systemFontOfSize:16];
    [antarctica addTarget:self action:@selector(Antarctica) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:antarctica];


    [self collectionView];

    /**< 搜索 */
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(search)];

}
#pragma mark - 搜索
- (void)search
{
    QpSearchViewController *p = [[QpSearchViewController alloc]init];
    UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:p];
    [self presentViewController:navi animated:YES completion:^{
        
    }];
}
#pragma mark - 南极洲按钮触发方法
- (void)Antarctica
{
    
    [self dataHander:@"759"];
    
}
#pragma mark - 大洋洲按钮触发方法
- (void)Oceania
{
    [self dataHander:@"239"];
}
#pragma mark - 北美洲按钮触发方法
- (void)AmericaS
{
    [self dataHander:@"235"];
}
#pragma mark - 南美洲按钮触发方法
- (void)AmericaN
{
    [self dataHander:@"234"];
}
#pragma mark - 非洲按钮触发方法
- (void)Africa
{
    [self dataHander:@"76"];
    
    
}
#pragma mark - 欧洲按钮触发方法
- (void)Europe
{
    [self dataHander:@"12"];
    
}
#pragma mark - 亚洲按钮触发方法
- (void)Asia1
{
    
    [self dataHander:@"10"];
}
#pragma mark - collectionView
- (void)collectionView
{
    
    // flowLayout 流式布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // 滚动方向
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    // cell 大小
    layout.itemSize = CGSizeMake((WIDTH - 30) / 2, 540 / 2);
    // 行列间距
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    // 边界
    layout.sectionInset = UIEdgeInsetsMake(5, 10, 10, 10);
    self.collecionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 285, self.view.frame.size.width, 300) collectionViewLayout:layout];
    self.collecionView.backgroundColor = [UIColor whiteColor];
    
    _collecionView.delegate = self;
    _collecionView.dataSource = self;

    
    [_headerView addSubview:_collecionView];
    
    // 重用池
    [_collecionView registerClass:[OMGSiteCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];


    
}
#pragma mark - collectionView 实现方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _hot_countryArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    OMGSiteModel *m = _hot_countryArr[indexPath.item];
    OMGSiteCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    //   cell.backgroundColor = [UIColor redColor];
   // [cell NightWithType:UIViewColorTypeNormal];
    cell.cnnameLabel.text = m.cnname;
    cell.cnnameLabel.font = [UIFont systemFontOfSize:13];
    cell.ennameLabel.text = m.enname;
    cell.ennameLabel.font = [UIFont systemFontOfSize:12];
    cell.countLabel.text = [m.count stringValue];
    cell.countLabel.font = [UIFont systemFontOfSize:14];
    cell.labelLabel.text = m.label;
    cell.labelLabel.font = [UIFont systemFontOfSize:13];
    cell.cnnameLabel.textColor = [UIColor whiteColor];
    cell.ennameLabel.textColor = [UIColor whiteColor];
    cell.countLabel.textColor = [UIColor whiteColor];
    cell.labelLabel.textColor = [UIColor whiteColor];
    
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:m.photo]placeholderImage:[UIImage imageNamed:@"zhanweitu.jpg"]];
    return cell;
}
#pragma mark - collectionCell 跳转方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    OMCollectionViewController *collectionVC = [[OMCollectionViewController alloc] init];
    OMGSiteModel *d = _hot_countryArr[indexPath.item];
    collectionVC.cid = [d.cid stringValue];
    collectionVC.label = d.label;
    [self.navigationController pushViewController:collectionVC animated:YES];
//    [collectionVC release];
}
#pragma mark - tableView 实现方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _country.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    OMGSiteModel *m = _country[indexPath.row];
    OMGSIteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    //[cell NightWithType:UIViewColorTypeNormal];
    cell.textLabel.text = [NSString stringWithFormat:@"%@  %@",m.cnname,m.enname];
    
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    //    cell.detailTextLabel.text = m.enname;
    //    cell.enname.textColor = [UIColor colorWithRed:85 / 255.0 green:85 / 255.0 blue:85 / 255.0 alpha:1];
    return cell;
    
    
    
}
#pragma mark - tableViewCell 跳转方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    OMGSiteModel *m = _country[indexPath.row];
    OMCollectionViewController *tableVC = [[OMCollectionViewController alloc] init];
    
    tableVC.cid = [m.cid stringValue];
    [self.navigationController pushViewController:tableVC animated:YES];
    
    

    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45;
}


-(void)dataHander:(NSString *)cid
{
    NSString *str = @"http://open.qyer.com/qyer/footprint/continent_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.88241944084977&lon=121.5398896720327&page=1&track_app_channel=App%2520Store&track_app_version=6.8.3&track_device_info=iPhone7%2C2&track_deviceid=0C79B4F1-2C09-4E9A-805E-05836431AB22&track_os=ios%25209.2&v=1";
    [QPNetWorkingTool getNetWorkingTool:str dic:nil success:^(id result) {
        NSArray *arr = result[@"data"];
        self.hot_countryArr = [NSMutableArray array];
        for (NSDictionary *dic in arr) {
            //判断
            if ([cid isEqualToString:[dic[@"id"] stringValue]]) {
                self.hot_countryArr = [OMGSiteModel modelHanderWithArray: dic[@"hot_country"]];
                
                self.country = [OMGSiteModel modelHanderWithArray:dic[@"country"]];
            }
            
        }
        [_collecionView reloadData];
        [_tableView reloadData];

    } fail:^(NSError *error) {
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
